package com.example.maple.ccsclient.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.adapter.LackAdapter;
import com.example.maple.ccsclient.interfaces.ILackView;
import com.example.maple.ccsclient.model.LackModel;
import com.example.maple.ccsclient.presenter.LackPresenter;
import com.example.maple.ccsclient.utils.SharedPreferencesUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import java.nio.channels.spi.AbstractSelectionKey;
import java.util.ArrayList;
import java.util.List;

public class LackActivity extends Activity implements ILackView {


	LackPresenter mLackPresenter;

	private Animation anim;
	private ImageButton mImageButton;

	LackAdapter mLackAdapter = null;

	PullToRefreshGridView mPullToRefreshGridView = null;
	List<LackModel> mLackModelList = null;
	int mColumn ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lack);
		mColumn = SharedPreferencesUtils.getCharColumn(this);
		mLackPresenter = new LackPresenter(this, this);
		findView();
	}

	private void findView() {
		mLackModelList = new ArrayList<>();
		mLackAdapter = new LackAdapter(this, R.layout.lack_item, mLackModelList, mColumn);
		mPullToRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_to_refresh);
		mPullToRefreshGridView.setAdapter(mLackAdapter);
		mPullToRefreshGridView.getRefreshableView().setNumColumns(mColumn);

		mImageButton = (ImageButton) findViewById(R.id.refresh);
		anim = AnimationUtils.loadAnimation(LackActivity.this, R.anim.refresh_rotate);
		mImageButton.startAnimation(anim);
		anim.cancel();
		mImageButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//刷新数据
				mLackPresenter.getLack(true);
			}
		});


		mPullToRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {

			//下拉刷新
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<GridView> pullToRefreshBase) {
				//刷新数据
				mLackPresenter.getLack(true);
			}

			//上滑加载
			@Override
			public void onPullUpToRefresh(PullToRefreshBase<GridView> pullToRefreshBase) {
				//加载数据
				mLackPresenter.getLack(false);
			}
		});
		//刷新数据
		mLackPresenter.getLack(true);
	}

	public static void startAction(Context context, String par) {
		Intent intent = new Intent(context, LackActivity.class);
		intent.putExtra("info", par);
		context.startActivity(intent);
	}

	//显示
	@Override
	public void showLack(List<LackModel> list, boolean isRefresh) {
		mPullToRefreshGridView.onRefreshComplete();
		if (isRefresh) {
			mLackModelList.clear();
		}
		mLackModelList.addAll(list);
		mLackAdapter.notifyDataSetChanged();
	}

	//开始刷新
	@Override
	public void startAnimation() {
		if (null != anim) {
			anim.start();
		}
	}

	//停止刷新动画
	@Override
	public void stopAnimation() {
		if (null != anim) {
			anim.cancel();
		}
	}

	@Override
	public void showDialog(String toastText) {
		Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show();
	}


	@Override
	protected void onPause() {
		super.onPause();
		if (null != anim) {
			anim.cancel();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (null != anim) {
			anim.cancel();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (null != anim) {
			anim.cancel();
		}
	}
}
